package Server;

import module.ExamQuestions;
import module.Student;

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ServerManageQuestions {

    private static List<ExamQuestions> examQuestions = new ArrayList<>();
    private static List<Student> students = new ArrayList<>();

    public static ObjectInputStream objectInput;
    public static ObjectOutputStream objectOutput;
    private static int checkflag;

    /**
     * description: 读取学生信息
     * @return void
     */
    public static void readFile(){
        try {
            objectInput = new ObjectInputStream(new FileInputStream("d:/test/listQuestions.txt"));
            Object obj = objectInput.readObject();
            examQuestions = (List<ExamQuestions>) obj;
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (null != objectInput) {
                try {
                    objectInput.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * description:写入到文件更新信息
     * @return void
     */
    public static void writeFile(){
        try {
            objectOutput = new ObjectOutputStream(new FileOutputStream("d:/test/listQuestions.txt"));
            objectOutput.writeObject(examQuestions);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != objectOutput) {
                try {
                    objectOutput.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    /**
     * description: 考题管理系统
     *
     * @param server 服务器
     * @return void
     */
    public static void work(ServerInitClose server) throws IOException, ClassNotFoundException {

        while (true) {
            readFile();
            String str = (String) server.getIn().readObject();
            String flag = str.split(";")[0];
            //增加考题
            if ("add".equals(flag)) {
                String title = str.split(";")[1];
                String choose = str.split(";")[2];
                String answer = str.split(";")[3];
                examQuestions.add(new ExamQuestions(title,choose,answer));
                writeFile();
                server.getOut().writeObject("success");
                //删除考题
            } else if ("delete".equals(flag)) {
                checkflag = 0;
                String title =str.split(";")[1];
                Iterator<ExamQuestions> iterator = examQuestions.iterator();
                // 判断是否还有迭代对象
                while (iterator.hasNext()) {
                    // 获取学生对象
                    ExamQuestions student = iterator.next();
                    // 判断学生对象的学号和输入的学号是否相同
                    if (student.getTitle().contains(title)) {
                        iterator.remove();
                        writeFile();
                        System.out.println("删除成功");
                        checkflag = 1;
                    }
                }
                if(0 == checkflag){
                    server.getOut().writeObject("fail");
                }else {
                    server.getOut().writeObject("success");
                }
                //查找考题
            } else if ("find".equals(flag)) {
                checkflag = 0;
                String title =str.split(";")[1];
                for (ExamQuestions examQuestions : examQuestions) {
                    // 判断学生对象中的姓名和查找的姓名是否相同
                    if (examQuestions.getTitle().contains(title)) {
                        server.getOut().writeObject("success");
                        server.getOut().writeObject(examQuestions);
                        System.out.println("查找成功！");;
                        checkflag = 1;
                    }
                }
                if(0 == checkflag){
                    server.getOut().writeObject("fail");
                }
                server.getOut().writeObject("end");
                System.out.println("查找完毕");
                //修改考题
            } else if ("modify".equals(flag)) {
                checkflag = 0;
                String oldtitle =str.split(";")[1];
                String  title = str.split(";")[2];
                String choose =str.split(";")[3];
                String answer =str.split(";")[4];
                for (ExamQuestions student : examQuestions) {
                    if (student.getTitle().contains(oldtitle)) {
                        student.setTitle(title);
                        student.setChoose(choose);
                        student.setAnswer(answer);

                        checkflag = 1;
                        writeFile();
                    }
                }if(0 == checkflag){
                    server.getOut().writeObject("fail");
                }else {
                    server.getOut().writeObject("success");
                }
                //查询所有考题
            }else if ("for".equals(flag)) {
                server.getOut().writeObject(examQuestions);
                System.out.println("发送完毕！");
                //退出系统
            }else if("end".equals(flag)){
                return;
            }
        }
    }

}
